package com.colorfruit.admin.control.controller;

import com.colorfruit.admin.business.service.ConfigService;
import com.colorfruit.admin.pojo.entity.GlobalProcessionControl;
import com.colorfruit.admin.pojo.enums.ErrorCodeEnum;
import com.colorfruit.framework.model.pojo.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/config")
public class ConfigController {

    @Autowired
    ConfigService configService;

    /**
     * 获取全局进度
     * @return
     */
    @GetMapping("/getGlobalProcession")
    public BaseResult getGlobalProcession(){
        try {
            log.info("# [IN] [controller] getGlobalProcession param:");
            GlobalProcessionControl globalProcession = configService.getGlobalProcession();
            log.info("# [OUT] [controller] getGlobalProcession SUCCESS");
            return new BaseResult(globalProcession);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 全局进度变更
     * @return
     */
    @GetMapping("/setGlobalProcession")
    public BaseResult setGlobalProcession(Byte globalProcessionStatus){
        try {
            log.info("# [IN] [controller] setGlobalProcession param: globalProcessionStatus = {}", globalProcessionStatus);
            BaseResult baseResult = configService.setGlobalProcession(globalProcessionStatus);
            log.info("# [OUT] [controller] setGlobalProcession SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 设置热销产品数量
     * @param hotSaleNum
     * @return
     */
    @GetMapping("/setHotSaleProductNums")
    public BaseResult setHotSaleProductNums(int hotSaleNum){
        try {
            log.info("# [IN] [controller] setGlobalProcession param: hotSaleNum = {}", hotSaleNum);
            BaseResult baseResult = configService.setHotSaleProductNums(hotSaleNum);
            log.info("# [OUT] [controller] setGlobalProcession SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 设置运输成本
     * @param transportCost
     * @return
     */
    @GetMapping("/setTransportCost")
    public BaseResult setTransportCost(String transportCost){
        try {
            log.info("# [IN] [controller] setTransportCost param: transportCost = {}", transportCost);
            BaseResult baseResult = configService.setTransportCost(transportCost);
            log.info("# [OUT] [controller] setTransportCost SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 设置配送成本
     * @param distributionCost
     * @return
     */
    @GetMapping("/setDistributionCost")
    public BaseResult setDistributionCost(String distributionCost){
        try {
            log.info("# [IN] [controller] setDistributionCost param: distributionCost = {}", distributionCost);
            BaseResult baseResult = configService.setDistributionCost(distributionCost);
            log.info("# [OUT] [controller] setDistributionCost SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /**
     * 进度条重置
     * @return
     */
    @GetMapping("/restartConfig")
    public BaseResult restartConfig(){
        log.info("# [IN] [controller] restartConfig param: ");
        BaseResult baseResult = configService.restartConfig();
        log.info("# [OUT] [controller] restartConfig SUCCESS");
        return baseResult;
    }

    /**
     * 设置下单开始时间
     * @param time
     * @return
     */
    @GetMapping("/setStartShopTime")
    public BaseResult setStartShopTime(String time){
        log.info("# [IN] [controller] setStartShopTime param: time = {}", time);
        BaseResult baseResult = configService.setStartShopTime(time);
        log.info("# [OUT] [controller] setStartShopTime SUCCESS");
        return baseResult;
    }

    /**
     * 设置下单结束时间
     * @param time
     * @return
     */
    @GetMapping("/setStopShopTime")
    public BaseResult setStopShopTime(String time){
        log.info("# [IN] [controller] setStopShopTime param: time = {}", time);
        BaseResult baseResult = configService.setStopShopTime(time);
        log.info("# [OUT] [controller] setStopShopTime SUCCESS");
        return baseResult;
    }

    /**
     * 设置进度条重置时间
     * @param time
     * @return
     */
    @GetMapping("/restartGlobalConfigTime")
    public BaseResult restartGlobalConfigTime(String time){
        log.info("# [IN] [controller] restartGlobalConfigTime param: time = {}", time);
        BaseResult baseResult = configService.restartGlobalConfigTime(time);
        log.info("# [OUT] [controller] restartGlobalConfigTime SUCCESS");
        return baseResult;
    }

    /**
     * 上传文件
     * @param mediaType 媒体类型 1 图片  2 视频
     * @param mediaCode 媒体编码 前端确定
     * @param ossUrl oss的URL
     * @param forceReplace 如果该code的媒体编码已存在是否替换 1 替换 2 不替换
     * @return
     */
    @GetMapping("/saveMedia")
    public BaseResult saveMedia(Byte mediaType, String mediaCode, String ossUrl, Byte forceReplace){
        log.info("# [IN] [controller] saveMedia param: mediaType = {}, mediaCode = {}, ossUrl = {}, forceReplace = {}",
                mediaType, mediaCode, ossUrl, forceReplace);
        BaseResult baseResult = configService.saveMedia(mediaType, mediaCode, ossUrl, forceReplace);
        log.info("# [OUT] [controller] saveMedia SUCCESS");
        return baseResult;
    }

    /**
     * 通过编码获取文件
     * @param mediaCode 媒体编码
     * @return
     */
    @GetMapping("/getMedia")
    public BaseResult getMedia(String mediaCode){
        log.info("# [IN] [controller] getMedia param: mediaCode = {}", mediaCode);
        BaseResult baseResult = configService.getMedia(mediaCode);
        log.info("# [OUT] [controller] getMedia SUCCESS");
        return baseResult;
    }

}
